<?php
namespace Fwadmin\Controller;
use Think\Controller;
class CategoryController extends BaseController{
	public function index(){
		$model		= M('Model') -> select();
		$category	= M('Category') -> order('order_id desc')->select();
		$category	= toLevel($category,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',0);
		$this		-> assign('model',$model);
		$this		-> assign('category',$category);
		$this		-> display();
	}
	public function add(){
		$model			= M('Model') -> select();
		$parent_id		= I('parent_id',0);
		$category_info	= M('Category') -> where(array('id' => $parent_id)) -> find();
		if($category_info){
			$this		-> assign('parent_id',$parent_id);
		}
		$category		= M('category') -> order('order_id desc') -> select();
		$category		= toLevel($category,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',0);
		$this			-> assign('model',$model);
		$this			-> assign('category',$category);
		$this			-> display('add');
	}
	public function edit(){
		$model			= M('Model') -> select();
		$id				= I('id');
		$categoryInfo	= M('Category') -> where(array('id' => $id)) -> find();
		$category		= M('category') -> order('order_id desc') -> select();
		$category		= toLevel($category,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',0);
		$this			-> assign('model',$model);
		$this			-> assign('category',$category);
		$this			-> assign('categoryInfo',$categoryInfo);
		$this			-> display('edit');
	}
	public function flag(){
		$Data = D('Category');
		$data['flag'] = trim(I('flag'));
		if($data['flag'] != ""){
			if(preg_match("/^[0-9a-zA-Z]{3,30}$/",$data['flag'])){
				$If_flag = $Data -> where($data) -> select();
				if($If_flag){
					$date['msg']	= '重复标识！';
					$date['status']	= '0';
					$this->ajaxReturn($date);
				}else{
					$date['msg']	= '验证通过！';
					$date['status']	= '1';
					$this->ajaxReturn($date);
				}
			}else{
				$date['msg']	= '只能是英文字母或数字,且长度必须是3-30个字！';
				$date['status']	= '0';
				$this->ajaxReturn($date);
			}
		}else{
			$date['msg']	= '标识不能为空！';
			$date['status']	= '0';
			$this->ajaxReturn($date);
		}
	}
}